﻿using Oracle.ManagedDataAccess.Client;
using Server.Constant;
using Server.Enum;
using Server.OptExp;
using Server.Framework.Message;
using Server.Framework.Processor;
using Server.Framework.Service;
using Server.Framework.Socket;
using Server.Message.Request;
using Server.Message.Response;
using Server.Model;
using System.Data.Common;
using System;

namespace Server.Processor
{
    /// <summary>
    /// Author: Leil
    /// Description: 新增或编辑菜单
    /// CreateTime: 2020/5/13 10:12:49
    /// </summary>
    [ProcessorAttr(MessageId.ADD_EDIT_MENU)]
    class AddEditMenuProcessor : IProcessor
    {

        public void Process(ProtobufAppSession session, RequestBaseMessage message)
        {
            if (session.User == null)
            {
                Console.WriteLine("用户未登录");
                return;
            }
            if (message is AddEditMenuReq msg)
            {
                OperationResp resp = new OperationResp(MessageId.OPT_RESP);
                if (msg.Menu == null)
                {
                    resp.Code = 500;
                    resp.OptType = (int)OptType.ADD_EDIT_MENU;
                    resp.Content = "菜单不能为空";
                    session.Send(resp);
                    return;
                } else
                {
                    try
                    {
                        if (msg.Menu.ID != 0)
                        {
                            EditMenu(msg.Menu, session);
                        }
                        else
                        {
                            AddMenu(msg.Menu, session);
                        }
                    }
                    catch (CommonOptException e)
                    {
                        resp.Code = 500;
                        resp.OptType = (int)OptType.ADD_EDIT_MENU;
                        resp.Content = e.Message;
                        session.Send(resp);
                    }
                }
            }
        }


        /// <summary>
        /// 新增菜单
        /// </summary>
        /// <param name="menu">菜单</param>
        /// <param name="resp">消息</param>
        private void AddMenu(MenuDao menu, ProtobufAppSession session)
        {
            if (string.IsNullOrEmpty(menu.MenuName)) {
                throw new CommonOptException("菜单名称不能为空");
            }
            string sql = $@"INSERT INTO SYS_MENU 
( ID, PARENT_ID, ICON, SCREEN_CODE, MENU_NAME, MENU_TYPE, STATUS, SORT, CREATOR, CREATE_TIME )
VALUES
( seq_sys_menu.NEXTVAL, {menu.ParentId}, '{menu.Icon}', '{menu.ScreenCode}', '{menu.MenuName}', {menu.MenuType}, {menu.Status}, {menu.Sort}, '{session.User.LoginName}', SYSDATE )";
            
            DBService.Instance().ExecuteNonQuery(sql, res => {
                OperationResp resp = new OperationResp(MessageId.OPT_RESP);
                if (res > 0)
                {
                    resp.Code = 200;
                    resp.OptType = (int)OptType.ADD_EDIT_MENU;
                    resp.Content = "新增成功";
                    session.Send(resp);
                } else
                {
                    resp.Code = 500;
                    resp.OptType = (int)OptType.ADD_EDIT_MENU;
                    resp.Content = "新增失败";
                    session.Send(resp);
                }
            });
        }

        /// <summary>
        /// 编辑菜单
        /// </summary>
        /// <param name="menu">菜单</param>
        /// <param name="resp">消息</param>
        private void EditMenu(MenuDao menu, ProtobufAppSession session)
        {
            string sql = $@"UPDATE SYS_MENU 
SET PARENT_ID = {menu.ParentId},
ICON = '{menu.Icon}',
SCREEN_CODE = '{menu.ScreenCode}',
MENU_NAME = '{menu.MenuName}',
MENU_TYPE = {menu.MenuType},
STATUS = {menu.Status},
SORT = {menu.Sort},
UPDATOR = '{session.User.LoginName}',
UPDATE_TIME = SYSDATE 
WHERE
	ID = {menu.ID}";

            DBService.Instance().ExecuteNonQuery(sql, res => {
                OperationResp resp = new OperationResp(MessageId.OPT_RESP);
                if (res > 0)
                {
                    resp.Code = 200;
                    resp.OptType = (int)OptType.ADD_EDIT_MENU;
                    resp.Content = "更新成功";
                    session.Send(resp);
                }
                else
                {
                    resp.Code = 500;
                    resp.OptType = (int)OptType.ADD_EDIT_MENU;
                    resp.Content = "更新失败";
                    session.Send(resp);
                }
            });
        }


    }
}
